#include "CwGauge.h"
#include "cw_gauge_draw.h"

/* prototype declarations */
PtWidgetClass_t *CreateCwGaugeClass (void);

/* widget class pointer - class structure, create function */
PtWidgetClassRef_t WCwGauge = { NULL, CreateCwGaugeClass };
PtWidgetClassRef_t *CwGauge = &WCwGauge; 


//-----------------------------------------------------------------------------
// загрузка значений по умолчанию
//-----------------------------------------------------------------------------
static void cwgauge_dflts (PtWidget_t *widget)
{
	CwGaugeWidget_t		*gauge 	= ( CwGaugeWidget_t * ) widget;
//	PtBasicWidget_t		*basic 	= ( PtBasicWidget_t * ) widget;

	gauge->max = 10;
	gauge->min = 0;
	gauge->pos = 0;
	
	gauge->str_fmt = strdup ("%.1f");
		
	gauge->n_div = 4;
	gauge->n_subdiv = 5;
	gauge->n_zone = 3;
	gauge->zone = calloc (3, sizeof (zone_t));
	
	gauge->zone [0].color = Pg_GREEN;	
	gauge->zone [1].color = Pg_YELLOW;	
	gauge->zone [2].color = Pg_RED;	
	
	gauge->zone [0].min = 0;
	gauge->zone [0].max = 2;
	
	gauge->zone [1].min = 2;
	gauge->zone [1].max = 7;

	gauge->zone [2].min = 7;
	gauge->zone [2].max = 10;
}

//-----------------------------------------------------------------------------
// функция прорисовки всего widget'a
//-----------------------------------------------------------------------------
static void cwgauge_draw (PtWidget_t *widget, PhTile_t *damage)
{
	PhRect_t			canvas;

	PdOffscreenContext_t *osc = NULL;
	PhDrawContext_t *dc;
	

	PtCalcCanvas (widget, &canvas);

	osc = PdCreateOffscreenContext (0, 
			canvas.lr.x - canvas.ul.x + 1,
			canvas.lr.y - canvas.ul.y + 1,
			0);//Pg_OSC_MEM_2D_WRITABLE);
	dc = PhDCSetCurrent(osc);
	PdSetOffscreenTranslation (osc, &canvas.ul);
	if (osc == NULL) printf ("cwgauge: error osc\n");
		
	PgSetDrawMode (Pg_DRAWMODE_OPAQUE);
	PtSuperClassDraw (PtBasic, widget, damage );
	//PtClipAdd (widget, &canvas);

	PgSetStrokeColor (Pg_BLACK);
	PgDrawRect (&canvas, Pg_DRAW_STROKE);
	
	cw_gauge_pos_draw (widget, &canvas);  
	cw_gauge_scale_draw (widget, &canvas);  

	//PtClipRemove ();
	
	PhDCSetCurrent (dc);	
	PgContextBlit (osc, &canvas, dc, &canvas);
	PgWaitHWIdle ();	
	PgFlush ();
	PhDCRelease (osc);
}
//-----------------------------------------------------------------------------

static void cwgauge_calc_opaque (PtWidget_t *widget)
{
//	PtBasicWidget_t *basic = (PtBasicWidget_t *) widget;

	// must have RECTANGULAR class flag set
	if (widget->class_rec->flags & Pt_RECTANGULAR) {
		widget->flags |= Pt_OPAQUE;
	}
}


//-----------------------------------------------------------------------------
// создание класса
//-----------------------------------------------------------------------------
PtWidgetClass_t *CreateCwGaugeClass( void )
{
	// define our resources
	static PtResourceRec_t resources[] = {
		{GAUGE_ARG_POS, Pt_CHANGE_REDRAW, 0, 
			Pt_ARG_IS_STRUCT (CwGaugeWidget_t, pos), 0},
		{GAUGE_ARG_MIN, Pt_CHANGE_REDRAW, 0, 
			Pt_ARG_IS_STRUCT (CwGaugeWidget_t, min), 0},		
		{GAUGE_ARG_MAX, Pt_CHANGE_REDRAW, 0, 
			Pt_ARG_IS_STRUCT (CwGaugeWidget_t, max), 0},
		{GAUGE_ARG_NDIV, Pt_CHANGE_REDRAW, 0, 
			Pt_ARG_IS_NUMBER (CwGaugeWidget_t, n_div), 0},
		{GAUGE_ARG_NSUBDIV, Pt_CHANGE_REDRAW, 0, 
			Pt_ARG_IS_NUMBER (CwGaugeWidget_t, n_subdiv), 0},
		{GAUGE_ARG_STRFMT, Pt_CHANGE_REDRAW, 0, 
			Pt_ARG_IS_STRING (CwGaugeWidget_t, str_fmt), 0},
			
		{GAUGE_ARG_ZONES, Pt_CHANGE_REDRAW, 0, 
			Pt_ARG_IS_ARRAY (CwGaugeWidget_t, zone),
			Pt_ARG_IS_NUMBER (CwGaugeWidget_t, n_zone)},
	};

	// set up our class member values
	static PtArg_t args[] = {
		{ Pt_SET_VERSION, 110},
		{ Pt_SET_STATE_LEN, sizeof (CwGaugeWidget_t) },
		{ Pt_SET_DFLTS_F, (long)cwgauge_dflts },
		{ Pt_SET_DRAW_F, (long)cwgauge_draw },
		{ Pt_SET_CALC_OPAQUE_F, (long)cwgauge_calc_opaque },
//		{ Pt_SET_FLAGS, 0, Pt_RECTANGULAR },
		{ Pt_SET_NUM_RESOURCES, sizeof (resources)/sizeof (resources [0])},
		{ Pt_SET_RESOURCES, (long)resources, sizeof (resources)/sizeof (resources [0])},
	};

	// create the widget class
	return (CwGauge->wclass = PtCreateWidgetClass (
		PtBasic, 0, sizeof (args)/sizeof (args[0]), args));
}